package cn.cas.mango.dto.param;

import cn.cas.mango.util.StringUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties({"wrappedContent"})

public class News {
  // 发生时间
  private String fssj;
  // 关键词
  private Object keywords;
  // 来源省市
  private Object lyss;
  // key
  private String nm;
  // 标题
  private String sjmc;
  // 内容
  private String sjms;
  // 所属领域
  private Object ssly;
  @JsonProperty(value = "entities")
  private Object entities;
  @JsonProperty(value = "entities_organization")
  private Object entitiesOrganization;
  @JsonProperty(value = "entities_place")
  private Object entitiesPlace;
  @JsonProperty(value = "entities_person")
  private Object entitiesPerson;

  @JsonProperty(value = "cluster_entities")
  private Object clusterEntities;
  @JsonProperty(value = "cluster_entities_organization")
  private Object clusterEntitiesOrganization;
  @JsonProperty(value = "cluster_entities_place")
  private Object clusterEntitiesPlace;
  @JsonProperty(value = "cluster_entities_person")
  private Object clusterEntitiesPerson;

  private String cluster_id1;
  private String cluster_id3;
  private String cluster_id5;
  private String countryOrArea;

  public String borrowWrappedContent() {
    if (sjms == null) {
      return "";
    } return sjms;
  }

  public News filterKeywords(Set<String> blackList) {
    if (keywords instanceof Map) {
      keywords = new ArrayList<Map>(){{add((Map)keywords);}};
    } else if (keywords == null) {
      keywords = new ArrayList<>();
    }
    keywords = ((List<Map>) keywords).stream().filter(x->!blackList.contains(x.get("name") ) &&
        StringUtil.judgeValidKeywords((String)x.get("name"))).collect(Collectors.toList());
    return this;
  }

  public List<String> getFilteredKeywords(Set<String> blackList) {
    if (keywords instanceof Map) {
      keywords = new ArrayList<Map>(){{add((Map)keywords);}};
    } else if (keywords == null) {
      keywords = new ArrayList<>();
    }
    keywords = ((List<Map>) keywords).stream().filter(x->!blackList.contains(x.get("name"))).collect(Collectors.toList());
    return ((List<Map>) keywords).stream().map(x->(String)x.get("name")).collect(Collectors.toList());
  }

}
